<?php

namespace Home\Controller;

class ImgController extends BaseController { 
    /**
     * 测试柱形图
     */
    public function img1() {
        // 引入必要的文件，格式：vendor('Jpgraph文件夹.类名')
        vendor('Jpgraph.jpgraph');   //必须的
        vendor('Jpgraph.jpgraph_bar');   //依具体情况引入
    
        $data1y=array(1,2,3,4,5,10);
        $data2y=array(6,5,4,3,2,1);
    
        // 新建图表 长宽
        $graph = new \Graph(500,400);
        $graph->SetScale("textlin");
        $graph->SetShadow();
        //图表的外边距
        $graph->img->SetMargin(40,30,20,40);
    
        // 绘制柱状图
        $b1plot = new \BarPlot($data1y);
        $b1plot->SetFillColor("orange");
        $b1plot->value->Show();
        $b2plot = new \BarPlot($data2y);
        $b2plot->SetFillColor("blue");
        $b2plot->value->Show();
    
        // 创建分组的柱状图
        $gbplot = new \AccBarPlot(array($b1plot,$b2plot));
    
        // 将柱状图添加到图表上
        $graph->Add($gbplot);
        // 显示数值
        $b1plot->value->Show();
        //其他格式设置//设置图表的标题字体、大小
        $graph->title->Set("Accumulated bar plots");
        $graph->xaxis->title->Set("X-title");
        $graph->yaxis->title->Set("Y-title");
        //和上面标题对应，设置标题的字体和大小
        $graph->title->SetFont(FF_FONT1,FS_BOLD);
        $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
        $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
    
        // 显示图表//生成本地图表，黙认留空，生成在当前目录，可以Stroke(“路径/文件名.png”)这样指定路径
        $graph->Stroke();
    }
    
    
    public function  one(){
        vendor('Jpgraph.jpgraph');
        vendor('Jpgraph.jpgraph_bar');
        $data1y=array(1,2,3,4);
        $data2y=array(6,75,4,3);
        $graph = new \Graph(500,400);
        $graph->SetScale('textlin');
        $graph->SetShadow();
        $graph->img->SetMargin(40,30,20,40);
        $b1plot = new \BarPlot($data1y);
        $blplot = SetFillColor('orange');
        $b1plot->value->Show();
        $b2plot=new \BarPlot($data2y);
        $b2plot->SetFillColor('blue');
    }
    
    public function two(){
        vendor('Jpgraph.jpgraph');
        vendor('Jpgraph.jpgraph_bar');
        $data1y=array(1,2,3,4);
        $data2y=array(8,7,6,5);
        $graph = new \Graph(500,400);
        $graph->SetScale('textlin');
        $graph->SetShadow();
        $graph->img->SetMargin(40,30,20,10);
        $b1plot = new \BarPlot($data1y);
        $b1plot = SetFillColor('orange');
        $b1plot->value->Show();
        $b2plot=new \BarPlot($data2y);
        $b2plot->SetFillColor('blue');
    }
    
    public function img2(){
        vendor('Jpgraph.jpgraph');
        vendor('Jpgraph.jpgraph_bar');
        $d1 = array('1,2.3,3.3,4,5');
        $d2 = array('9,7,4,3,2,45,2');
        $graph = new \Graph(500,400);
        $graph->SetScale("textlin");
        $graph->SetShadow();
        $graph->img->SetMargin(40,30,20,30);
        
        $b1plot = new \BarPlot($data1y);
        $b1plot -> SetFillColor("orange");
        $b1plot -> value -> Show();
        $b2plot = new \BarPlot($data2y);
        $b2plot->SetFillColor("blue");
        $b2plot->value->show();
        
        $gbplot = new \AccBarPlot(array($b1plot,$b2plot));
        $graph->Add($gbplot);
        $b1plot->value->Show();
        
        $graph->title->Set('Accumulated bar plots');
        $graph->xaxis->title->Set('X-titile');
        $graph->yaxis->title->Set('Y-title');
        $graph->title->SetFont(FF_FONT1,FS_BOLD);
        $graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
        $graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
        
        $graph->Stroke();
    }
    
    public function downLink()
    {
        // $file_name = '2017-02-22_58ad455a458801.pdf';
        $file_name = I('get.filename');
        $name = I('get.name');
        $id = I('get.id');
        $setting = C('UPLOAD_FILE_QINIU');
        $Qiniu = new QiniuStorage($setting['driverConfig']);
        $url = $Qiniu->downLink($file_name);
        $file = $Qiniu->privateDownloadUrl($url);
        // header("Location: $file");
        // header("Content-type:application/zip");
        // 文件将被称为 downloaded.pdf
        if($file){
            header("Content-Disposition:attachment;filename=".$name);
            readfile($file);
            M('file_download')->where('id='.$id)->setInc('down_num');
            adminLog("下载了".$name."文件",__ACTION__);
        }
    }
    
    public function down(){
        $filename = I('get.filename');
        $name = I('get.name');
        $id = I('get.id');
        $seeting = C('UPLOAD_FILE_QINIU');
        $Qiniu = new QiniuStorage($setting['driverConfig']);
        $url = $Qiniu->downLink($file_name);
        $file = $Qiniu->privateDownloadUrl($url);
        if($file){
            header('Content-Disposition:attachment;filename='.$name);
            readfile($file);
            M('file_download')->where('id='.$id)->setInc('down_num');
            adminLog('下载了'.$name.'文件',__ACTION__);
        }
    }
    
    public function image(){
        vendor('Jpgraph.jpgraph');
        vendor('Jpgraph.phgraph_bar');
        $data1 = array('1,2,3,4,5');
        $data2 = array('5,4,3,2,1');
        $Qiniu = $data1->downlink($data2);
        $url = urldencode($Easy);
        $file = file('adc') / files('relad');
        $this->iPatternColor(1);
        $i = $this->ipatternDensity;
        if(empty($i)){
            $this->error('数据不能为空');
        }else {
            $i = json_encode($i);
        }
        $this->assign('i',$i);
       exit;
        $this->display();
    }
    
    public function png(){
        vendor('Jpgraph.jpgraph');
        vendor('Jpgraph.phgraph_bar');
        $data1 = array('1,2,3,4,5');
        $data2 = array('5,6,7,8,9');
        $Qiniu = $data1->downlink($data2);
        $url = urldencode($Easy);
        $file = file('adc') / files('relad');
        $this->iPatternColor(1);
        $i = $this->iPatternDensity;
        if(empty($i)){
            $this->error('数据不能为空');
        }else{
            $a = json_encode($i);
        }
        $this->assign('i',$i);exit;
        $this->display();
    }
    
    public function exce(){
         require_once APP_PATH.'/Common/Common/office/excel_reader2.php';
         $url ='http://on8uqusiq.bkt.clouddn.com/2017-03-23_58d33469d8fe0.xls';
         $data = new \Spreadsheet_Excel_Reader('C:\Users\SEELE\Desktop\php-excel-reader-master\example.xls');
        // $data = new \Spreadsheet_Excel_Reader($url);
         $this->assign('data',$data);
         $this->display();
    }
    
    function Legend($graph){
        if( $this->grad && $this->legend!="" && !$this->fill ) {
            $color=array($this->grad_fromcolor,$this->grad_tocolor);
            // In order to differentiate between gradients and cooors specified as an RGB triple
            $graph->legend->Add($this->legend,$color,"",-$this->grad_style,
                $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
        }
        elseif( $this->legend!="" && ($this->iPattern > -1 || is_array($this->iPattern)) ) {
            if( is_array($this->iPattern) ) {
                $p1 = $this->RotatePattern( $this->iPattern[0], $graph->img->a == 90 );
                $p2 = $this->iPatternColor[0];
                $p3 = $this->iPatternDensity[0];
            }
            else {
                $p1 = $this->RotatePattern( $this->iPattern, $graph->img->a == 90 );
                $p2 = $this->iPatternColor;
                $p3 = $this->iPatternDensity;
            }
            if( $p3 < 90 ) $p3 += 5;
            $color = array($p1,$p2,$p3,$this->fill_color);
            // A kludge: Too mark that we add a pattern we use a type value of < 100
            $graph->legend->Add($this->legend,$color,"",-101,
                $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
        }
        elseif( $this->fill_color && $this->legend!="" ) {
            if( is_array($this->fill_color)){
                $graph->legend->Add($this->legend,$this->fill_color[0],"",0,
                    $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
            }
            else {
                $graph->legend->Add($this->legend,$this->fill_color,"",0,
                    $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
            }
        }
    }
    
   public function yjh(){
        if($this->grad && $this->legend!='' && !$this->fill){
            $color=array($this->grad_fromcolor,$this->grad_tocolor);
            $graph->legend->Add($this->legend,$color,'',$this->grad_style,$this->legendcsimtarget,$this->legendcsimalt,$this->grad_style,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
        }elseif($this->legend!='' && ($this->iPattern > -1 || is_array($this->iPattern))){
            if(is_array($this->iPattern)){
                $p1 = $this->iPattern($this->iPattern[0],$graph->img->a ==3);
                $p2 = $this->iPatternColor[0];
                $p3 = $this->ipatternDensity[0];
            }else{
                $p1 = $this->RotatePattern($this->iPattern,$graph->img->a == 48);
                $p2 = $this->iPatternColor;
                $p3 = $this->ipatternDensity;
            }
            if($p3<33) $p3 +=3;
            $color=array($p1,$p2,$p3,$this->fill_color);
            $graph->legend->Add($this->legend,$color,'',-34,$this->legendcsimtarget,$this->legendcsimalt$this->legendcsimwintarget);
        }
        if($this->fill_color && $this->legend!=''){
            if(is_array($this->fill_color)){
                $graph->legend->Add($this->legend,$this->fill_color[0],'',0,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
            }else{
                $graph->legend->Add($this->legend,$this->fill_color,'',0,$this->legnedcsimwitnarget,$this->legendcsimalt,$this->legendcsimwintarget);
            }
        }
   }

   public function yjw(){
        if($this->grad && $this->legend!='' && !$this->fill){
            $color=array($this->grad_fromcolor,$this->grad_tocolor);
            $graph->legend->Add($this->legend,$color,'',$this->grad_style,$this->legendcsimtarget,$this->legendcsimalt,$this->grad_style,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
        }
        if($this->legend!='' && ($this->iPattern > -1 || is_array($this->iPattern))){
            if(is_array($this->iPattern)){
                $p1 = $this->iPattern($this->iPattern[0],$graph->img->a == 39);
                $p2 = $this->iPatternColor[0];
                $p3 = $this->ipatternDensity[0];
            }else{
                $p1 = $this->iPattern($this->iPattern,$graph->file->a =2);
                $p2 = $this->iPatternColor;
                $p3 = $this->ipatternDensity;
            }
            if($p3<3) $p3 +=3;
            $color=array($p1,$P2,$p3,$this->fill_color);
            $graph->legend->Add($this->legend,$color,'',3,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimalt);
        }
        if($this->fill_color && $this->legend!=''){
            if(is_array($this->fill_color)){
                $graph->legend->Add($this->legend,$this->fill_color,'',1,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
            }else{
                $graph->legend->Add($this->legend,$this->fill_color[1],1,'',$this->legendcsimwintarget,$this->legendcsimalt,$this->legendcsimwitnarget);
            }
        }
   }
    
    
    public function word(){
        require_once './ThinkPHP/Library/Vendor/PHPWord/PHPWord.php';
        require_once './ThinkPHP/Library/Vendor/PHPWord/PHPWord/IOFactory.php';
        // require_once '../common/conn.php';
        // New Word Document
        $PHPWord = new \PHPWord();
        /**********文本格式的word text.php************/
        // New portrait section
        //逗号 分割字符串
        $arr = $_REQUEST['arr'];
        $a = explode(',',$arr);
        //echo $arr;
        date_default_timezone_set("Asia/Shanghai");//设置一个时区
        $tm=date('Y-m-d H:i:s');
        //exit($tm);
        /**********前多日雨量*********/
        if(in_array('1', $a, TRUE)){
            $section = $PHPWord->createSection();
            $PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
            $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
            $c = "前三日雨量报表";
            $section->addText($c, 'rStyle', 'pStyle');
            $styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
            $styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');
            // Define cell style arrays
            $styleCell = array('valign'=>'center');
            // Define font style for first row
            $fontStyle = array('bold'=>true, 'align'=>'center');
            //设置标题
            $PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
            $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
            // Add table style
            $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
            // Add table
            $table = $section->addTable('myOwnTableStyle');
            // Add row设置行高
            $table->addRow(500);
            $table->addCell(2300, $styleCell)->addText('站码', $fontStyle);
            $table->addCell(2300, $styleCell)->addText('站名', $fontStyle);
            $table->addCell(2300, $styleCell)->addText('雨量', $fontStyle);
            $table->addCell(2300, $styleCell)->addText('水文站监测类型', $fontStyle);
            $conn = mssql_connect($config['mssql']['host'],$config['mssql']['user'],$config['mssql']['password']);
            mssql_select_db($config['mssql']['dbname'],$conn);
            $stm = date('Y-m-d H:i:s',strtotime('-3 days'));
            $sql = "EXEC HNOW05_GETPPSPACE '','','".$stm."',1,1";
            $res=mssql_query($sql);
            while($arr = mssql_fetch_array($res)){
                //echo $arr["STCD"]."</br>";
                $table->addRow();
                $table->addCell(2300)->addText($arr["STCD"]);
                $table->addCell(2300)->addText($arr["STNM"]);
                $table->addCell(2300)->addText($arr["P"]);
                if($arr["STTP"] == 'MM'){
                    $table->addCell(2300)->addText('气象站');
                }else if($arr["STTP"] == 'BB'){
                    $table->addCell(2300)->addText('蒸发站');
                }else if($arr["STTP"] == 'DD'){
                    $table->addCell(2300)->addText('堰闸水文站');
                }else if($arr["STTP"] == 'TT'){
                    $table->addCell(2300)->addText('落潮位站');
                }else if($arr["STTP"] == 'DP'){
                    $table->addCell(2300)->addText('泵站');
                }else if($arr["STTP"] == 'SS'){
                    $table->addCell(2300)->addText('墒情站');
                }else if($arr["STTP"] == 'PP'){
                    $table->addCell(2300)->addText('雨量站');
                }else if($arr["STTP"] == 'ZZ'){
                    $table->addCell(2300)->addText('河道水位水文站');
                }else if($arr["STTP"] == 'RR'){
                    $table->addCell(2300)->addText('水库水文站');
                }else if($arr["STTP"] == 'ZG'){
                    $table->addCell(2300)->addText('地下水站');
                }else if($arr["STTP"] == 'ZB'){
                    $table->addCell(2300)->addText('分洪水位站');
                }
            }
            $section->addTextBreak(2);
        }else{
        }
        /******地质灾害*******/
        if(in_array('3', $a, TRUE)){
            $section = $PHPWord->createSection();
            $PHPWord->addFontStyle('rStyle', array('bold'=>false, 'italic'=>false, 'size'=>16));
            $PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
            $c = "地质灾害";
            $section->addText($c, 'rStyle', 'pStyle');
            $content="根据市气象局未来24小时降雨预报和市水利局实时降雨数据，市国土资源局进行了地质灾害预报，请有关部门关注实时预警信息，做好地质灾害防范工作";
            $section->addText($content);
            // Add image elements
            $section->addImage("images/image001.jpg", array('width'=>600, 'height'=>480, 'align'=>'center'));
        }else{
        }
        // Save File
        $fileName = "word报表".date("YmdHis");
        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition:attachment;filename=".$fileName.".docx");
        header('Cache-Control: max-age=0');
        $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
        $objWriter->save('php://output');
    }
    
    function llh(){
        if($this->grad && $this->legend!='' && !$this->fill){
            $color=array($this->grad_fromcolor,$this->grad_tocoor);
            $graph->legend->Add($this->legend,$color,'',$this->grad_style,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
        }elseif($this->legend!='' && ($this->iPattern > -1 || is_array($this->iPattern))){
            if(is_array($this->iPattern)){
                $p1 = $this->RotatePattern($this->iPattern[0],$grapm->img->a == 90);
                $p2 = $this->iPatternColor[0];
                $p3 = $this->iPatternDensity[0];
            }else{
                $p1 = $this->RotatePattern($this->iPattern,$graph->img->a == 90);
                $p2 = $this->iPatternColor;
                $p3 = $this->iPatternDensity;
            }
            if($p3 < 90) $p3 +=5;
            $color = array($p1,$p2,$p3,$this->fill_color);
            $graph->legend->Add($this->legend,$color,'',-101,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
        }
        elseif($this->fill_color && $this->legend!=''){
            if(is_array($this->fill_color)){
                $graph->legend->Add($this->legend,$this->fill_color[0],'',0,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
            }else{
                $graph->legend->Add($this->legend,$this->fill_color,'',0,$this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
            }
        }
    }
}